home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / karchive.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  20.7 KB  |  640 lines

  1. /* This file is part of the KDE libraries
  2.    Copyright (C) 2000 David Faure <faure@kde.org>
  3.    Copyright (C) 2003 Leo Savernik <l.savernik@aon.at>
  4.  
  5.    Moved from ktar.h by Roberto Teixeira <maragato@kde.org>
  6.  
  7.    This library is free software; you can redistribute it and/or
  8.    modify it under the terms of the GNU Library General Public
  9.    License version 2 as published by the Free Software Foundation.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.    Library General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU Library General Public License
  17.    along with this library; see the file COPYING.LIB.  If not, write to
  18.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19.    Boston, MA 02110-1301, USA.
  20. */
  21. #ifndef __karchive_h
  22. #define __karchive_h
  23.  
  24. #include <sys/stat.h>
  25. #include <sys/types.h>
  26.  
  27. #include <qdatetime.h>
  28. #include <qstring.h>
  29. #include <qstringlist.h>
  30. #include <qdict.h>
  31.  
  32. #include <kdelibs_export.h>
  33.  
  34. class KArchiveDirectory;
  35. class KArchiveFile;
  36.  
  37. /**
  38.  * KArchive is a base class for reading and writing archives.
  39.  * @short generic class for reading/writing archives
  40.  * @author David Faure <faure@kde.org>
  41.  */
  42. class KIO_EXPORT KArchive
  43. {
  44. protected:
  45.     /**
  46.      * Base constructor (protected since this is a pure virtual class).
  47.      * @param dev the I/O device where the archive reads its data
  48.      * Note that this can be a file, but also a data buffer, a compression filter, etc.
  49.      */
  50.     KArchive( QIODevice * dev );
  51.  
  52. public:
  53.     virtual ~KArchive();
  54.  
  55.     /**
  56.      * Opens the archive for reading or writing.
  57.      * Inherited classes might want to reimplement openArchive instead.
  58.      * @param mode may be IO_ReadOnly or IO_WriteOnly
  59.      * @see close
  60.      */
  61.     virtual bool open( int mode );
  62.  
  63.     /**
  64.      * Closes the archive.
  65.      * Inherited classes might want to reimplement closeArchive instead.
  66.      *
  67.      * @see open
  68.      */
  69.     virtual void close();
  70.  
  71.     /**
  72.      * Use to check if close had any problem
  73.      * @return true if close succeded without problems
  74.      * @since 3.5
  75.      */
  76.     // TODO KDE4 merge with above
  77.     bool closeSucceeded() const;
  78.  
  79.     /**
  80.      * Checks whether the archive is open.
  81.      * @return true if the archive is opened
  82.      */
  83.     bool isOpened() const { return m_open; }
  84.  
  85.     /**
  86.      * Returns the mode in which the archive was opened
  87.      * @return the mode in which the archive was opened (IO_ReadOnly or IO_WriteOnly)
  88.      * @see open()
  89.      */
  90.     int mode() const { return m_mode; }
  91.  
  92.     /**
  93.      * The underlying device.
  94.      * @return the underlying device.
  95.      */
  96.     QIODevice * device() const { return m_dev; }
  97.  
  98.     /**
  99.      * If an archive is opened for reading, then the contents
  100.      * of the archive can be accessed via this function.
  101.      * @return the directory of the archive
  102.      */
  103.     const KArchiveDirectory* directory() const;
  104.  
  105.     /**
  106.      * Writes a local file into the archive. The main difference with writeFile,
  107.      * is that this method minimizes memory usage, by not loading the whole file
  108.      * into memory in one go.
  109.      *
  110.      * If @p fileName is a symbolic link, it will be written as is, i. e.
  111.      * it will not be resolved before.
  112.      * @param fileName full path to an existing local file, to be added to the archive.
  113.      * @param destName the resulting name (or relative path) of the file in the archive.
  114.      */
  115.     bool addLocalFile( const QString& fileName, const QString& destName );
  116.  
  117.     /**
  118.      * Writes a local directory into the archive, including all its contents, recursively.
  119.      * Calls addLocalFile for each file to be added.
  120.      *
  121.      * Since KDE 3.2 it will also add a @p path that is a symbolic link to a
  122.      * directory. The symbolic link will be dereferenced and the content of the
  123.      * directory it is pointing to added recursively. However, symbolic links
  124.      * *under* @p path will be stored as is.
  125.      * @param path full path to an existing local directory, to be added to the archive.
  126.      * @param destName the resulting name (or relative path) of the file in the archive.
  127.      */
  128.     bool addLocalDirectory( const QString& path, const QString& destName );
  129.  
  130.     /**
  131.      * If an archive is opened for writing then you can add new directories
  132.      * using this function. KArchive won't write one directory twice.
  133.      *
  134.      * @param name the name of the directory
  135.      * @param user the user that owns the directory
  136.      * @param group the group that owns the directory
  137.      *
  138.      * @todo TODO(BIC): make this a thin wrapper around
  139.      * writeDir(name,user,group,perm,atime,mtime,ctime)
  140.      * or eliminate it
  141.      */
  142.     virtual bool writeDir( const QString& name, const QString& user, const QString& group ) = 0;
  143.  
  144.     /**
  145.      * If an archive is opened for writing then you can add new directories
  146.      * using this function. KArchive won't write one directory twice.
  147.      *
  148.      * This method also allows some file metadata to be
  149.      * set. However, depending on the archive type not all metadata might be
  150.      * regarded.
  151.      * @param name the name of the directory
  152.      * @param user the user that owns the directory
  153.      * @param group the group that owns the directory
  154.      * @param perm permissions of the directory
  155.      * @param atime time the file was last accessed
  156.      * @param mtime modification time of the file
  157.      * @param ctime creation time of the file
  158.      * @since 3.2
  159.      * @todo TODO(BIC): make this virtual. For now use virtual hook
  160.      */
  161.     bool writeDir( const QString& name, const QString& user, const QString& group,
  162.                 mode_t perm, time_t atime, time_t mtime, time_t ctime );
  163.  
  164.     /**
  165.      * Writes a symbolic link to the archive if the archive must be opened for
  166.      * writing.
  167.      * @param name name of symbolic link
  168.      * @param target target of symbolic link
  169.      * @param user the user that owns the directory
  170.      * @param group the group that owns the directory
  171.      * @param perm permissions of the directory
  172.      * @param atime time the file was last accessed
  173.      * @param mtime modification time of the file
  174.      * @param ctime creation time of the file
  175.      * @since 3.2
  176.      * @todo TODO(BIC) make virtual. For now it must be implemented by virtual_hook.
  177.      */
  178.     bool writeSymLink(const QString &name, const QString &target,
  179.                 const QString &user, const QString &group,
  180.                 mode_t perm, time_t atime, time_t mtime, time_t ctime);
  181.  
  182.     /**
  183.      * If an archive is opened for writing then you can add a new file
  184.      * using this function. If the file name is for example "mydir/test1" then
  185.      * the directory "mydir" is automatically appended first if that did not
  186.      * happen yet.
  187.      * @param name the name of the file
  188.      * @param user the user that owns the file
  189.      * @param group the group that owns the file
  190.      * @param size the size of the file
  191.      * @param data the data to write (@p size bytes)
  192.      * @todo TODO(BIC): make this a thin non-virtual wrapper around
  193.      *   writeFile(name,user,group,size,perm,atime,mtime,ctime,data)
  194.      */
  195.     virtual bool writeFile( const QString& name, const QString& user, const QString& group, uint size, const char* data );
  196.  
  197.     /**
  198.      * If an archive is opened for writing then you can add a new file
  199.      * using this function. If the file name is for example "mydir/test1" then
  200.      * the directory "mydir" is automatically appended first if that did not
  201.      * happen yet.
  202.      *
  203.      * This method also allows some file metadata to be
  204.      * set. However, depending on the archive type not all metadata might be
  205.      * regarded.
  206.      * @param name the name of the file
  207.      * @param user the user that owns the file
  208.      * @param group the group that owns the file
  209.      * @param size the size of the file
  210.      * @param perm permissions of the file
  211.      * @param atime time the file was last accessed
  212.      * @param mtime modification time of the file
  213.      * @param ctime creation time of the file
  214.      * @param data the data to write (@p size bytes)
  215.      * @since 3.2
  216.      * @todo TODO(BIC): make virtual. For now use virtual hook
  217.      */
  218.     bool writeFile( const QString& name, const QString& user, const QString& group,
  219.                 uint size, mode_t perm, time_t atime, time_t mtime,
  220.                    time_t ctime, const char* data );
  221.  
  222.     /**
  223.      * Here's another way of writing a file into an archive:
  224.      * Call prepareWriting, then call writeData()
  225.      * as many times as wanted then call doneWriting( totalSize ).
  226.      * For tar.gz files, you need to know the size before hand, since it is needed in the header.
  227.      * For zip files, size isn't used.
  228.      *
  229.      * @param name the name of the file
  230.      * @param user the user that owns the file
  231.      * @param group the group that owns the file
  232.      * @param size the size of the file
  233.      *
  234.      * @todo TODO(BIC): make this a thin non-virtual wrapper around
  235.      * prepareWriting(name,user,group,size,perm,atime,mtime,ctime)
  236.      * or eliminate it.
  237.      */
  238.     virtual bool prepareWriting( const QString& name, const QString& user, const QString& group, uint size ) = 0;
  239.  
  240.     /**
  241.      * Here's another way of writing a file into an archive:
  242.      * Call prepareWriting, then call writeData()
  243.      * as many times as wanted then call doneWriting( totalSize ).
  244.      * For tar.gz files, you need to know the size before hand, it is needed in the header!
  245.      * For zip files, size isn't used.
  246.      *
  247.      * This method also allows some file metadata to be
  248.      * set. However, depending on the archive type not all metadata might be
  249.      * regarded.
  250.      * @param name the name of the file
  251.      * @param user the user that owns the file
  252.      * @param group the group that owns the file
  253.      * @param size the size of the file
  254.      * @param perm permissions of the file
  255.      * @param atime time the file was last accessed
  256.      * @param mtime modification time of the file
  257.      * @param ctime creation time of the file
  258.      * @since 3.2
  259.      * @todo TODO(BIC): make this virtual. For now use virtual hook.
  260.      */
  261.     bool prepareWriting( const QString& name, const QString& user,
  262.                 const QString& group, uint size, mode_t perm,
  263.                    time_t atime, time_t mtime, time_t ctime );
  264.  
  265.     /**
  266.      * Write data into the current file - to be called after calling prepareWriting
  267.      * @todo TODO(BIC) make virtual. For now virtual_hook allows reimplementing it.
  268.      */
  269.     bool writeData( const char* data, uint size );
  270.  
  271.     /**
  272.      * Call doneWriting after writing the data.
  273.      * @param size the size of the file
  274.      * @see prepareWriting()
  275.      */
  276.     virtual bool doneWriting( uint size ) = 0;
  277.  
  278. protected:
  279.     /**
  280.      * Opens an archive for reading or writing.
  281.      * Called by open.
  282.      * @param mode may be IO_ReadOnly or IO_WriteOnly
  283.      */
  284.     virtual bool openArchive( int mode ) = 0;
  285.  
  286.     /**
  287.      * Closes the archive.
  288.      * Called by close.
  289.      */
  290.     virtual bool closeArchive() = 0;
  291.  
  292.     /**
  293.      * Retrieves or create the root directory.
  294.      * The default implementation assumes that openArchive() did the parsing,
  295.      * so it creates a dummy rootdir if none was set (write mode, or no '/' in the archive).
  296.      * Reimplement this to provide parsing/listing on demand.
  297.      * @return the root directory
  298.      */
  299.     virtual KArchiveDirectory* rootDir();
  300.  
  301.     /**
  302.      * Ensures that @p path exists, create otherwise.
  303.      * This handles e.g. tar files missing directory entries, like mico-2.3.0.tar.gz :)
  304.      * @param path the path of the directory
  305.      * @return the directory with the given @p path
  306.      */
  307.     KArchiveDirectory * findOrCreate( const QString & path );
  308.  
  309.     /**
  310.      * @internal for inherited constructors
  311.      */
  312.     void setDevice( QIODevice *dev );
  313.  
  314.     /**
  315.      * @internal for inherited classes
  316.      */
  317.     void setRootDir( KArchiveDirectory *rootDir );
  318.  
  319. private:
  320.     QIODevice * m_dev;
  321.     bool m_open;
  322.     char m_mode;
  323. protected:
  324.     virtual void virtual_hook( int id, void* data );
  325.     /* @internal for virtual_hook */
  326.     enum { VIRTUAL_WRITE_DATA = 1, VIRTUAL_WRITE_SYMLINK, VIRTUAL_WRITE_DIR,
  327.         VIRTUAL_WRITE_FILE, VIRTUAL_PREPARE_WRITING };
  328.     bool prepareWriting_impl( const QString& name, const QString& user,
  329.                 const QString& group, uint size, mode_t perm,
  330.                 time_t atime, time_t mtime, time_t ctime );
  331.     struct PrepareWritingParams {
  332.     const QString *name;
  333.     const QString *user;
  334.     const QString *group;
  335.     uint size;
  336.     mode_t perm;
  337.     time_t atime, mtime, ctime;
  338.     bool retval;
  339.     };
  340.     bool writeFile_impl( const QString& name, const QString& user,
  341.                 const QString& group, uint size, mode_t perm,
  342.                 time_t atime, time_t mtime, time_t ctime,
  343.                    const char* data );
  344.     struct WriteFileParams {
  345.     const QString *name;
  346.     const QString *user;
  347.     const QString *group;
  348.     uint size;
  349.     mode_t perm;
  350.     time_t atime, mtime, ctime;
  351.     const char *data;
  352.     bool retval;
  353.     };
  354.     bool writeDir_impl(const QString& name, const QString& user,
  355.                 const QString& group, mode_t perm,
  356.                 time_t atime, time_t mtime, time_t ctime);
  357.     struct WriteDirParams {
  358.     const QString *name;
  359.     const QString *user;
  360.     const QString *group;
  361.     mode_t perm;
  362.     time_t atime, mtime, ctime;
  363.     bool retval;
  364.     };
  365.     bool writeSymLink_impl(const QString &name, const QString &target,
  366.                 const QString &user, const QString &group,
  367.                 mode_t perm, time_t atime, time_t mtime, time_t ctime);
  368.     struct WriteSymlinkParams {
  369.     const QString *name;
  370.     const QString *target;
  371.     const QString *user;
  372.     const QString *group;
  373.     mode_t perm;
  374.     time_t atime, mtime, ctime;
  375.     bool retval;
  376.     };
  377.     bool writeData_impl( const char* data, uint size );
  378.     struct WriteDataParams {
  379.         const char* data;
  380.         uint size;
  381.         bool retval;
  382.     };
  383. private:
  384.     class KArchivePrivate;
  385.     KArchivePrivate * d;
  386. };
  387.  
  388. /**
  389.  * A base class for entries in an KArchive.
  390.  * @short Base class for the archive-file's directory structure.
  391.  *
  392.  * @see KArchiveFile
  393.  * @see KArchiveDirectory
  394.  */
  395. class KIO_EXPORT KArchiveEntry
  396. {
  397. public:
  398.     /**
  399.      * Creates a new entry.
  400.      * @param archive the entries archive
  401.      * @param name the name of the entry
  402.      * @param access the permissions in unix format
  403.      * @param date the date (in seconds since 1970)
  404.      * @param user the user that owns the entry
  405.      * @param group the group that owns the entry
  406.      * @param symlink the symlink, or QString::null
  407.      */
  408.     KArchiveEntry( KArchive* archive, const QString& name, int access, int date,
  409.                const QString& user, const QString& group,
  410.                const QString &symlink );
  411.  
  412.     virtual ~KArchiveEntry() { }
  413.  
  414.     /**
  415.      * Creation date of the file.
  416.      * @return the creation date
  417.      */
  418.     QDateTime datetime() const;
  419.  
  420.     /**
  421.      * Creation date of the file.
  422.      * @return the creation date in seconds since 1970
  423.      */
  424.     int date() const { return m_date; }
  425.  
  426.     /**
  427.      * Name of the file without path.
  428.      * @return the file name without path
  429.      */
  430.     QString name() const { return m_name; }
  431.     /**
  432.      * The permissions and mode flags as returned by the stat() function
  433.      * in st_mode.
  434.      * @return the permissions
  435.      */
  436.     mode_t permissions() const { return m_access; }
  437.     /**
  438.      * User who created the file.
  439.      * @return the owner of the file
  440.      */
  441.     QString user() const { return m_user; }
  442.     /**
  443.      * Group of the user who created the file.
  444.      * @return the group of the file
  445.      */
  446.     QString group() const { return m_group; }
  447.  
  448.     /**
  449.      * Symlink if there is one.
  450.      * @return the symlink, or QString::null
  451.      */
  452.     QString symlink() const { return m_symlink; }
  453.  
  454.     /**
  455.      * Checks whether the entry is a file.
  456.      * @return true if this entry is a file
  457.      */
  458.     virtual bool isFile() const { return false; }
  459.  
  460.     /**
  461.      * Checks whether the entry is a directory.
  462.      * @return true if this entry is a directory
  463.      */
  464.     virtual bool isDirectory() const { return false; }
  465.  
  466. protected:
  467.     KArchive* archive() const { return m_archive; }
  468.  
  469. private:
  470.     QString m_name;
  471.     int m_date;
  472.     mode_t m_access;
  473.     QString m_user;
  474.     QString m_group;
  475.     QString m_symlink;
  476.     KArchive* m_archive;
  477. protected:
  478.     virtual void virtual_hook( int id, void* data );
  479. private:
  480.     class KArchiveEntryPrivate* d;
  481. };
  482.  
  483. /**
  484.  * Represents a file entry in a KArchive.
  485.  * @short A file in an archive.
  486.  *
  487.  * @see KArchive
  488.  * @see KArchiveDirectory
  489.  */
  490. class KIO_EXPORT KArchiveFile : public KArchiveEntry
  491. {
  492. public:
  493.     /**
  494.      * Creates a new file entry.
  495.      * @param archive the entries archive
  496.      * @param name the name of the entry
  497.      * @param access the permissions in unix format
  498.      * @param date the date (in seconds since 1970)
  499.      * @param user the user that owns the entry
  500.      * @param group the group that owns the entry
  501.      * @param symlink the symlink, or QString::null
  502.      * @param pos the position of the file in the directory
  503.      * @param size the size of the file
  504.      */
  505.     KArchiveFile( KArchive* archive, const QString& name, int access, int date,
  506.               const QString& user, const QString& group, const QString &symlink,
  507.               int pos, int size );
  508.  
  509.     virtual ~KArchiveFile() { }
  510.  
  511.     /**
  512.      * Position of the data in the [uncompressed] archive.
  513.      * @return the position of the file
  514.      */
  515.     int position() const; // TODO use Q_LONG in KDE-4.0
  516.     /**
  517.      * Size of the data.
  518.      * @return the size of the file
  519.      */
  520.     int size() const; // TODO use Q_LONG in KDE-4.0
  521.     /**
  522.      * Set size of data, usually after writing the file.
  523.      * @param s the new size of the file
  524.      */
  525.     void setSize( int s ) { m_size = s; }
  526.  
  527.     /**
  528.      * Returns the data of the file.
  529.      * Call data() with care (only once per file), this data isn't cached.
  530.      * @return the content of this file.
  531.      */
  532.     virtual QByteArray data() const;
  533.  
  534.     /**
  535.      * This method returns QIODevice (internal class: KLimitedIODevice)
  536.      * on top of the underlying QIODevice. This is obviously for reading only.
  537.      * Note that the ownership of the device is being transferred to the caller,
  538.      * who will have to delete it.
  539.      * The returned device auto-opens (in readonly mode), no need to open it.
  540.      * @return the QIODevice of the file
  541.      */
  542.     QIODevice *device() const; // TODO make virtual
  543.  
  544.     /**
  545.      * Checks whether this entry is a file.
  546.      * @return true, since this entry is a file
  547.      */
  548.     virtual bool isFile() const { return true; }
  549.  
  550.     /**
  551.      * Extracts the file to the directory @p dest
  552.      * @param dest the directory to extract to
  553.      * @since 3.1
  554.      */
  555.     void copyTo(const QString& dest) const;
  556.  
  557. private:
  558.     int m_pos; // TODO use Q_LONG in KDE-4.0
  559.     int m_size; // TODO use Q_LONG in KDE-4.0
  560. protected:
  561.     virtual void virtual_hook( int id, void* data );
  562. private:
  563.     class KArchiveFilePrivate* d;
  564. };
  565.  
  566. /**
  567.  * Represents a directory entry in a KArchive.
  568.  * @short A directory in an archive.
  569.  *
  570.  * @see KArchive
  571.  * @see KArchiveFile
  572.  */
  573. class KIO_EXPORT KArchiveDirectory : public KArchiveEntry
  574. {
  575. public:
  576.     /**
  577.      * Creates a new directory entry.
  578.      * @param archive the entries archive
  579.      * @param name the name of the entry
  580.      * @param access the permissions in unix format
  581.      * @param date the date (in seconds since 1970)
  582.      * @param user the user that owns the entry
  583.      * @param group the group that owns the entry
  584.      * @param symlink the symlink, or QString::null
  585.      */
  586.     KArchiveDirectory( KArchive* archive, const QString& name, int access, int date,
  587.                    const QString& user, const QString& group,
  588.                    const QString& symlink);
  589.  
  590.     virtual ~KArchiveDirectory() { }
  591.  
  592.     /**
  593.      * Returns a list of sub-entries.
  594.      * @return the names of all entries in this directory (filenames, no path).
  595.      */
  596.     QStringList entries() const;
  597.     /**
  598.      * Returns the entry with the given name.
  599.      * @param name may be "test1", "mydir/test3", "mydir/mysubdir/test3", etc.
  600.      * @return a pointer to the entry in the directory.
  601.      */
  602.     KArchiveEntry* entry( QString name );
  603.     /**
  604.      * Returns the entry with the given name.
  605.      * @param name may be "test1", "mydir/test3", "mydir/mysubdir/test3", etc.
  606.      * @return a pointer to the entry in the directory.
  607.      */
  608.     const KArchiveEntry* entry( QString name ) const;
  609.  
  610.     /**
  611.      * @internal
  612.      * Adds a new entry to the directory.
  613.      */
  614.     void addEntry( KArchiveEntry* );
  615.  
  616.     /**
  617.      * Checks whether this entry is a directory.
  618.      * @return true, since this entry is a directory
  619.      */
  620.     virtual bool isDirectory() const { return true; }
  621.  
  622.     /**
  623.      * Extracts all entries in this archive directory to the directory
  624.      * @p dest.
  625.      * @param dest the directory to extract to
  626.      * @param recursive if set to true, subdirectories are extracted as well
  627.      * @since 3.1
  628.      */
  629.      void copyTo(const QString& dest, bool recursive = true) const;
  630.  
  631. private:
  632.     QDict<KArchiveEntry> m_entries;
  633. protected:
  634.     virtual void virtual_hook( int id, void* data );
  635. private:
  636.     class KArchiveDirectoryPrivate* d;
  637. };
  638.  
  639. #endif
  640.